/**
 * Copyright (c) 2018-2021 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 *
 */


package com.kll.tv.views.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.kll.carolinehmibiz.common.constant.FontConstants;
import com.kll.tv.R;


/**
 * 具有蒙层按压效果的textview控件
 *
 * @author zlcai
 * @date 2022-02-28
 */

public class MantleTextView extends androidx.appcompat.widget.AppCompatTextView {

    private Context mContext;
    private Paint mBgPaint;
    private int mPressedMantleColor;

    public MantleTextView(Context context) {
        this(context, null);
    }

    public MantleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MantleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.mContext = context;
        initAttrs(attrs);
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.MantleTextView);

        // 获取字体路径
        int typefacePath = array.getInt(R.styleable.MantleTextView_gacTypeface, -1);

        switch (typefacePath) {
            case 0:
                setTypeface(FontConstants.getBoldTypeface(mContext));
                break;
            case 1:
                setTypeface(FontConstants.getMediumTypeFace(mContext));
                break;
            case 2:
                setTypeface(FontConstants.getHeavyTypeFace(mContext));
                break;
        }

        // 设置了蒙层点击效果才需要处理
        mPressedMantleColor = array.getColor(R.styleable.MantleTextView_pressedMantleColor, -1);

        if(mPressedMantleColor != -1) {

            float radius = array.getDimension(R.styleable.MantleTextView_pressedMantleRadius, 0f);
            if (radius > 0) {
                for (int i = 0; i < 8; i++) {
                    radiusArray[i] = radius;
                }
            } else {
                radiusArray[0] = array.getDimension(R.styleable.MantleTextView_pressedMantleTopLeftRadius, 0f);
                radiusArray[1] = radiusArray[0];
                radiusArray[2] = array.getDimension(R.styleable.MantleTextView_pressedMantleTopRightRadius, 0f);
                radiusArray[3] = radiusArray[2];
                radiusArray[4] = array.getDimension(R.styleable.MantleTextView_pressedMantleBottomRightRadius, 0f);
                radiusArray[5] = radiusArray[4];
                radiusArray[6] = array.getDimension(R.styleable.MantleTextView_pressedMantleBottomLeftRadius, 0f);
                radiusArray[7] = radiusArray[6];
            }

            createBgPaint();
        }
    }

    private boolean mPressedFlag = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPressedFlag = true;
                setPressed(true);
                invalidate();
                break;
            default:
                if((mPressedFlag && !isPressed()) || event.getAction() == MotionEvent.ACTION_UP) {
                    mPressedFlag = false;
                    invalidate();
                }
        }

        return super.onTouchEvent(event);
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        return super.onCreateDrawableState(extraSpace);
    }

    private void createBgPaint() {
        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgPaint.setColor(mPressedMantleColor);
    }


    @Override
    protected void onDraw(Canvas canvas) {
//        if(mPressedMantleColor != -1) {

//        }

        if(mPressedMantleColor != -1) {
            Path path = new Path();
            path.addRoundRect(new RectF(0, 0, getWidth(), getHeight()), radiusArray, Path.Direction.CW);
            canvas.clipPath(path);
            drawMantle(canvas);
        }

        super.onDraw(canvas);
    }

    private float[] radiusArray = { 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f };


    private void drawMantle(Canvas canvas) {

        //
        if(isPressed() && mPressedFlag && isEnabled()) {
            canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), 0, 0, mBgPaint);
        }
    }

 }
